//
//  TJPreviewEffectController.m
//  BaoDian
//
//  Created by mac on 2024/10/7.
//

#import "TJPreviewEffectController.h"
#import <WebKit/WebKit.h>
#import "TJVideoPlayController.h"


@interface TJPreviewEffectController ()<WKNavigationDelegate, WKUIDelegate, WKScriptMessageHandler>

@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, weak) UIActivityIndicatorView *activityIndicatorView;
@property (nonatomic, weak) UIImageView *playerIconView;

@end

@implementation TJPreviewEffectController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColor.blackColor;
    
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    configuration.applicationNameForUserAgent = getUserAgent();

    self.webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
    self.webView.navigationDelegate = self;
    self.webView.UIDelegate = self;
    self.webView.allowsBackForwardNavigationGestures = NO;
    [self.view addSubview:self.webView];
    self.webView.scrollView.backgroundColor = UIColor.blackColor;
    self.webView.backgroundColor = UIColor.blackColor;
    self.webView.scrollView.showsVerticalScrollIndicator = NO;
    self.webView.scrollView.showsHorizontalScrollIndicator = NO;
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.offset(0);
    }];
    
    UIImageView *playerIconView = [UIImageView addToView:self.view];
    playerIconView.image = [UIImage imageNamed:@"videoPlayer_icon"];
    [playerIconView addTapGesturesWithTarget:self action:@selector(tapPlayerView)];
    [playerIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
    }];
    
    _playerIconView = playerIconView;
    playerIconView.hidden = YES;

    UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite].addTo(self.view);
    activityIndicatorView.hidesWhenStopped = YES;
    [activityIndicatorView startAnimating];
    _activityIndicatorView = activityIndicatorView;
    [activityIndicatorView mas_makeConstraints:^(MASConstraintMaker *make) {make.center.offset(0);}];
    
//    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index.html" ofType:nil];
//    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
//    
    
//    [self.webView loadRequest:[NSURLRequest requestWithURL:@"https://www.baidu.com".URL]];

    [self.webView loadRequest:[NSURLRequest requestWithURL:@"https://rrbd-pc.waitiot.com/#/app/mobile".URL]];
}

- (void)tapPlayerView {
    
    
    [self.webView evaluateJavaScript:@"play()" completionHandler:^(id _Nullable x, NSError * _Nullable error) {
        NSLog(@"evaluateJavaScript = %@, error = %@", x, error.localizedDescription);
    }];
    
//   TJVideoPlayController *videoPlayController = [[TJVideoPlayController alloc] init];
//   videoPlayController.localPath = self.videoURL.path;
//   videoPlayController.coverImage = self.coverImage;
//   [self presentViewController:videoPlayController animated:YES completion:nil];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [self.activityIndicatorView stopAnimating]; 
    _playerIconView.hidden = NO;
    NSLog(@"%s", __func__);
    [self refresh];
}


- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
    NSLog(@"%s", __func__);
    [self.activityIndicatorView startAnimating];
}

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    NSLog(@"%s", __func__);
}

- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
    [self.activityIndicatorView stopAnimating];
    _playerIconView.hidden = NO;
    NSLog(@"%s", __func__);
    
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    NSLog(@"%s", __func__);
}

- (void)setMaterialDict:(NSDictionary *)materialDict {
    _materialDict = materialDict;

    [self.webView reload];
}

- (void)refresh {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
         NSString *jsonString = self.materialDict.mj_JSONString;
        [self.webView evaluateJavaScript:[NSString stringWithFormat:@"refresh('%@')", jsonString] completionHandler:^(id _Nullable x, NSError * _Nullable error) {
            NSLog(@"evaluateJavaScript = %@, error = %@", x, error.localizedDescription);
        }];
    });
}
 

@end
